﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Services;
using System.Configuration;
using PartyManagement.Models;
using Newtonsoft.Json;
using System.Runtime.Serialization;
using System.Web.Script.Serialization;

namespace PartyManagement.Controllers
{
    public class GroupMemberController : Controller
    {
        PartyManagementContext db = new PartyManagementContext();
        public ActionResult Index(int id = 0, string mess = null)
        {
            if (mess == "true")
                ViewBag.mess = "You have added this group successfully!";
            Event eve = db.Events.Find(id);
            if (eve == null)
            {
                return HttpNotFound();
            }
            return View(eve);
        }

        [HttpPost]
        public ActionResult Index(string groupName, string memberName, int eventID = 0)
        {
            if (groupName != null)
            {
                Group group = db.Groups.FirstOrDefault(p => p.groupName.Equals(groupName));
                var test = db.Attends.FirstOrDefault(p => p.userID == group.userID && p.eventID == eventID);
                List<UserInGroup> useringroup = db.UserInGroups.Where(p => p.groupID == group.groupID).ToList();
                Attend attend = new Attend();
                if (ModelState.IsValid)
                {
                    if (test == null)
                    {
                        foreach (var i in useringroup)
                        {
                            attend.userID = i.userID;
                            attend.eventID = eventID;
                            db.Attends.Add(attend);
                            db.SaveChanges();
                        }
                        ViewBag.mess = "Invite successfully!";
                    }
                    else
                    {
                        ViewBag.mess = "This group is invited!";
                    }
                }
            }
            UserProfile user = db.UserProfiles.FirstOrDefault(p => p.UserName.Equals(memberName));
            if (memberName != null)
            {
                var test = db.Attends.Where(p => p.userID == user.UserId && p.eventID == eventID);
                if (test == null)
                {
                    var userPro = db.UserProfiles.Where(p => p.UserName.Equals(memberName)).ToList();
                    if (userPro != null)
                    {
                        foreach (var i in userPro)
                        {
                            Attend attend = new Attend
                            {
                                eventID = eventID,
                                userID = i.UserId
                            };
                            db.Attends.Add(attend);
                            db.SaveChanges();
                        }
                        ViewBag.mess = "Invite successfully!";
                    }
                }
                else
                {
                    ViewBag.mess = "This memember is invited!";
                }
            }
            //return RedirectToAction("_inviteGroupPartial","GroupMember",new{mess=mess});
            Event eve = db.Events.Find(eventID);
            return View(eve);
        }

        public ActionResult showMember(int eventID = 0)
        {
            Attend attend = db.Attends.FirstOrDefault(p => p.eventID == eventID);
            var member = db.UserProfiles.Where(p => p.UserId == attend.userID).ToList();
            if (attend == null)
            {
                return HttpNotFound();
            }
            return View(member);
        }

        public JsonResult AutoCompleteGroup(string search)
        {
            string searchResult = string.Empty;
            var artists = (from a in db.Groups
                           where a.groupName.ToLower().StartsWith(search.ToLower())
                           select a.groupName).Take(10);
            var list = artists.ToList();
            JavaScriptSerializer js = new JavaScriptSerializer();
            string json = js.Serialize(list);
            return Json(json, JsonRequestBehavior.AllowGet);
        }

        public JsonResult AutoCompleteMember(string search)
        {
            string searchResult = string.Empty;


            var artists = (from a in db.UserProfiles
                           where a.UserName.ToLower().StartsWith(search.ToLower())
                           select a.UserName).Take(10);
            var list = artists.ToList();
            JavaScriptSerializer js = new JavaScriptSerializer();
            string json = js.Serialize(list);
            return Json(json, JsonRequestBehavior.AllowGet);
        }

        public PartialViewResult _inviteGroupPartial(int eventID)
        {
            return PartialView(eventID);
        }


        public PartialViewResult _inviteMemberPartial(int eventID)
        {
            return PartialView(eventID);
        }

        public ActionResult _showGroupPartial(string groupName)
        {
            Group group = db.Groups.FirstOrDefault(p => p.groupName.Equals(groupName));
            var useringroup = db.UserInGroups.Where(p => p.groupID == group.groupID).ToList();
            List<UserProfile> userprofile = db.UserProfiles.ToList();
            var user = from d1 in useringroup
                       join d2 in userprofile on d1.userID equals d2.UserId
                       select d2.UserName;
            var us = user.ToList();
            //JavaScriptSerializer js = new JavaScriptSerializer();
            //string json = js.Serialize(us);
            return Json(us, JsonRequestBehavior.AllowGet);
        }
    }
}
